using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.Data.Readers;

namespace TrainingManagement.api.Models.Data.CRUD.Employee
{
    public class EmployeeSelectDataSource : QuerySource<int?, List<BusinessObjects.Employee>>
    {
        public override List<BusinessObjects.Employee> Execute(int? request)
        {
            const string selectEmployee = "dbo.training_api_EmployeeSelect";
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@EmployeeID", SqlDbType.Int){Value = request};
                using (var rdr = SqlHelper.ExecuteReader(
                ConnectionStrings.TrainingDb,
                CommandType.StoredProcedure,
                selectEmployee,
                parameters))
                {
                    var results = new List<BusinessObjects.Employee>();
                    var empReader = new EmployeeDataReader();
                    while (rdr.Read())
                    {
                        results.Add(empReader.Run(rdr));
                    }
                    return results;
                }
        }
    }
}